using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace Money_Log_Mobile
{
    public class XML
    {
                public static string ApplicationPath
        {
            get { return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); ;            }
        }

        private string m_sFileName = "MoneyLogMobile.xml";
        //public void Write(List<Transaction> lstTransactions, List<StandingOrder> lstStandingOrders, List<string> Categories)
        //{
        //    XmlTextWriter xmlWriter = new XmlTextWriter(ApplicationPath + System.IO.Path.DirectorySeparatorChar + m_sFileName, System.Text.Encoding.UTF8);
        //    xmlWriter.WriteStartDocument();

        //    //write out the root element - Project
        //    xmlWriter.WriteStartElement("Preferences");

        //    xmlWriter.WriteAttributeString("", "FILENAME", "", m_sFileName);

        //    foreach (Transaction oTransaction in lstTransactions)
        //    {
        //        xmlWriter.WriteStartElement("Transaction");

        //        xmlWriter.WriteAttributeString("", "Name", "", oTransaction.Name);
        //        xmlWriter.WriteAttributeString("", "Date", "", oTransaction.Date.ToString());
        //        xmlWriter.WriteAttributeString("", "amount", "", oTransaction.Amount.ToString());
        //        xmlWriter.WriteAttributeString("", "Value", "", oTransaction.Categories.ToArray().ToString());
        //        xmlWriter.WriteEndElement();
        //    }

        //    xmlWriter.WriteEndElement();
        //    xmlWriter.WriteEndDocument();
        //    xmlWriter.Flush();
        //    xmlWriter.Close();
        //}
        public void Write(Double Balance, List<Transaction> lstTransactions, List<StandingOrder> lstStandingOrders)
        {
            XmlTextWriter xmlWriter = new XmlTextWriter(ApplicationPath + System.IO.Path.DirectorySeparatorChar + m_sFileName, System.Text.Encoding.UTF8);
            xmlWriter.WriteStartDocument();

            //write out the root element - Project
            xmlWriter.WriteStartElement("BANK");
            xmlWriter.WriteAttributeString("", "BALANCE", "", Balance.ToString());

                xmlWriter.WriteStartElement("Transactions");
                    foreach (Transaction oTransaction in lstTransactions)
                    {
                        xmlWriter.WriteStartElement("Transaction");
                        xmlWriter.WriteAttributeString("", "ID", "", oTransaction.TransactionID.ToString());
                        xmlWriter.WriteAttributeString("", "Name", "", oTransaction.Name);
                        xmlWriter.WriteAttributeString("", "Date", "", oTransaction.Date.ToString());
                        xmlWriter.WriteAttributeString("", "Amount", "", oTransaction.Amount.ToString());
                        //xmlWriter.WriteAttributeString("", "Categories", "", string.Join(",", oTransaction.Categories.ToArray()));
                        xmlWriter.WriteEndElement();
                    }
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("StandingOrders");
                    foreach (StandingOrder oStandingOrder in lstStandingOrders)
                    {
                        xmlWriter.WriteStartElement("StandingOrder");

                        xmlWriter.WriteAttributeString("", "Name", "", oStandingOrder.Name);
                        xmlWriter.WriteAttributeString("", "StartDate", "", oStandingOrder.StartDate.ToString());
                        xmlWriter.WriteAttributeString("", "Amount", "", oStandingOrder.Amount.ToString());
                        //xmlWriter.WriteAttributeString("", "Categories", "", string.Join(",", oStandingOrder.Categories.ToArray()));
                        xmlWriter.WriteEndElement();
                    }
                xmlWriter.WriteEndElement();

                xmlWriter.WriteStartElement("Categories");
                xmlWriter.WriteEndElement();

            xmlWriter.WriteEndElement();
            xmlWriter.WriteEndDocument();
            xmlWriter.Flush();
            xmlWriter.Close();
        }
        
        private List<Transaction> pi_lstTransactions = new List<Transaction>();
        private List<StandingOrder> pi_lstStandingOrders = new List<StandingOrder>();
        private List<string> pi_lstCategories = new List<string>();
        private double pi_Balance;

        public void Read(out List<Transaction> lstTransactions, out List<StandingOrder> lstStandingOrders,out List<string> lstCategories,out double Balance)
        {
            XmlTextReader reader = null;
            try
            {
                // Load the file with an XmlTextReader
                reader = new XmlTextReader(ApplicationPath + System.IO.Path.DirectorySeparatorChar + m_sFileName);

                // Process the supplied XML file
                FormatXml(reader, m_sFileName);
            }
            catch (Exception e)
            {
                string sMsg = e.Message;
            }
            finally
            {
                // Finished with XmlTextReader
                if (reader != null)
                {
                    reader.Close();
                }
            }//finally
            lstTransactions = pi_lstTransactions;
            lstStandingOrders = pi_lstStandingOrders;
            lstCategories = pi_lstCategories;
            Balance = pi_Balance;
        }
        private void FormatXml(XmlReader reader, String filename)
        {
            while (reader.Read())
            {
                if(reader.NodeType == XmlNodeType.Element)
                {
                        if (reader.Name == "Transaction")
                        {
                            Transaction oTransaction = new Transaction();
                            if (reader.HasAttributes)
                            {
                                while (reader.MoveToNextAttribute())
                                {
                                    if (reader.Name == "Name")
                                    {
                                        oTransaction.Name = reader.Value;
                                    }
                                    else if (reader.Name == "Amount")
                                    {
                                        oTransaction.Amount = Convert.ToDouble(reader.Value);
                                    }
                                    else if (reader.Name == "ID")
                                    {
                                        oTransaction.TransactionID = Convert.ToInt32(reader.Value);
                                    }
                                    else if (reader.Name == "Date")
                                    {
                                        oTransaction.Date = Convert.ToDateTime(reader.Value);
                                    }
                                    else if (reader.Name == "Categories")
                                    {
                                        //oTransaction.Categories.AddRange(reader.Value.Split(','));
                                    }
                                }
                            }
                            pi_lstTransactions.Add(oTransaction);
                        }
                        if (reader.Name == "StandingOrder")
                        {
                            StandingOrder oStandingOrder = new StandingOrder();
                            if (reader.HasAttributes)
                            {
                                while (reader.MoveToNextAttribute())
                                {
                                    if (reader.Name == "Name")
                                    {
                                        oStandingOrder.Name = reader.Value;
                                    }
                                    else if (reader.Name == "Amount")
                                    {
                                        oStandingOrder.Amount = Convert.ToDouble(reader.Value);
                                    }
                                    else if (reader.Name == "StartDate")
                                    {
                                        oStandingOrder.StartDate = Convert.ToDateTime(reader.Value);
                                    }
                                    else if (reader.Name == "Categories")
                                    {
                                        //oStandingOrder.Categories.AddRange(reader.Value.Split(','));
                                    }
                                    else if (reader.Name == "Frequency")
                                    {
                                        oStandingOrder.Frequency = (StandingOrderFrequency)Enum.Parse(typeof(StandingOrderFrequency), reader.Value, true);

                                    }
                                }
                            }
                            pi_lstStandingOrders.Add(oStandingOrder);
                        }
                        if (reader.Name == "BANK")
                        {
                            if (reader.HasAttributes)
                            {
                                while (reader.MoveToNextAttribute())
                                {
                                    if (reader.Name == "BALANCE")
                                    {
                                        pi_Balance = Convert.ToDouble(reader.Value);
                                    }
                                }
                            }
                        }







                }
            }
        }




    }
}
